<?php
namespace Admin\Controller;

class FriController extends AdminController
{
    public function index(){
    	$nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['id|title']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$nickname.'%');
        }
        $list   = $this->lists('Fri',$map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '友情链接信息';
        $this->display();
    }
    public function add(){
    	if(IS_POST){
            $fri=D('Fri');
            $res=$fri->create(I('post.'));
            if($res){
               if($fri->add()){
               	$this->success('添加成功',U('Fri/index'));
               }else{
               	$this->error('添加失败');
               }
            }else{
            $this->error($fri->getError());
            }
    	}else{
    		$this->display();
    	}
    }
    public function del(){
    	$fri=D('Fri');
    	$id=I('get.id');
    	$res=$fri->where(array('id'=>$id))->delete();
    		if($res){
    			$this->success('删除成功');
    		}else{
    			$this->error('删除失败');
    		}
    }
    public function update(){
    	$fri=D('Fri');
    	$id=I('get.id');
    	$res=$fri->where(array('id'=>$id))->select()[0];
    	$this->assign('res',$res);
    	if(IS_POST){
        $update=$fri->create(I('post.'));
        if($update){
           if($fri->where('id='.I('post.id'))->save()){
               $this->success('编辑成功',U('index'));
           }else{
           	   $this->error('编辑失败');
           }
        }else{
        	$this->error($fri->getError());
        }
    	}else{
    		$this->display();
    	}
    }
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('Fri', $map );
                break;
            case 'resumeuser':
                $this->resume('Fri', $map );
                break;
            case 'deleteuser':
                $this->delete('Fri', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
}